﻿using ASmile.Com;
using ASmile.Com.Report;
using ASmile.DBModel;
using ASmile.Win.Controls;
using System;
using System.Windows.Forms;

namespace ASmile.Win.Forms
{
    public partial class FrmCfgReport : FrmBase
    {
        public FrmCfgReport()
        {
            InitializeComponent();
        }

        IReportBLL BLLObj { get; } = ObjectFactory.GetObject<IReportBLL>("ReportBLL");

        ArgGetReport SearchArg { get; } = new ArgGetReport();

        ArgGetReport SearchDetail { get; } = new ArgGetReport();
        /// <summary>
        /// 是否快捷键进入
        /// </summary>
        bool IsKeyEnter { set; get; } = false;

        void RefreshReportData()
        {
            ReportPageDataChanged(1, ucPageNavReport.OnePageCount, out int total);
            ucPageNavReport.InitiControl(total);
        }

        void RefreshArgData()
        {
            var report = dgvReport.GetSelectedClassData<Sys_Report>();
            if (report == null)
            {
                dgvReportArg.DataSource = null;
                return;
            }
            SearchDetail.MainId = report.Id;
            dgvReportArg.DataSource = BLLObj.GetList(SearchDetail).data;
        }

        private void FrmReportConfig_Load(object sender, EventArgs e)
        {
            if (Owner is IFormContainer dgvContainer)
            {
                SearchArg.FormName = dgvContainer.ContainerName;
                tsBtnCopyAdd.Visible = tsBtnDelete.Visible = false;
                RefreshReportData();
                if (dgvReport.Rows.Count > 0)
                {
                    dgvReport.Rows[0].Selected = true;
                    tsBtnAdd.Visible = false;
                }
                IsKeyEnter = true;

                this.Text += $" - {dgvContainer.ContainerName}";
                RefreshArgData();
            }
            dgvReportArg.SetInputEdit<Sys_ReportArg>(Sys_ReportArg.CNOrderNo, txtOrderNo, SetCellEdit);
            dgvReportArg.SetDropMove<Sys_ReportArg>(SwapOrderNo);
            ucPageNavReport.PageChangedEvent += ReportPageDataChanged;
        }

        private void ReportPageDataChanged(int pageNo, int pageSize, out int total)
        {
            SearchArg.PageNo = pageNo;
            SearchArg.PageSize = pageSize;
            SearchArg.SearchVal = tsTxtSearch.Text;
            var ret = BLLObj.GetReportList(SearchArg);
            total = ret.count;
            dgvReport.DataSource = ret.data;
        }

        bool SetCellEdit(Sys_ReportArg m, EditText txt)
        {
            if (m.OrderNo == txtOrderNo.Value)
            {
                return false;
            }
            m.OrderNo = (int)txtOrderNo.Value;
            var ret = BLLObj.ChangeOrderNo(m);
            ShowMessage(ret.msg);
            return true;
        }

        void SwapOrderNo(Sys_ReportArg src, Sys_ReportArg tar)
        {
            ArgSwapOrderNo arg = new ArgSwapOrderNo
            {
                SrcId = src.Id,
                SrcOrderNo = src.OrderNo,
                TarId = tar.Id,
                TarOrderNo = tar.OrderNo
            };
            var ret = BLLObj.SwapOrderNo(arg);
            if (ret)
            {
                dgvReport.Refresh();
            }
            ShowMessage(ret.msg);
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            FrmCfgReportEdit frm = new FrmCfgReportEdit();
            var report = new Sys_Report();
            if (IsKeyEnter)
            {
                report.FormName = SearchArg.FormName;
            }
            var ret =
            frm.ShowDialog(new EditFormArgs
            {
                SetModel = report,
                SaveHandle = (m) => BLLObj.AddReport(m as Sys_Report)
            });
            if (ret)
            {
                report.Id = ret.data;
                dgvReport.AddDataSource(report);
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnCopyAdd_Click(object sender, EventArgs e)
        {
            var report = dgvReport.GetSelectedClassData<Sys_Report>();
            if (report == null)
            {
                ShowMessage("没有选中任何记录");
                return;
            }
            var reportSave = new Sys_Report();
            FrmCfgReportEdit frm = new FrmCfgReportEdit();
            var ret =
            frm.ShowDialog(new EditFormArgs
            {
                SetModel = report,
                SaveModel = reportSave,
                SaveHandle = (m) => BLLObj.AddReport(m as Sys_Report),
            });
            if (ret)
            {
                report.Id = ret.data;
                dgvReport.AddDataSource(reportSave);
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnEdit_Click(object sender, EventArgs e)
        {
            var report = dgvReport.GetSelectedClassData<Sys_Report>();
            if (report == null)
            {
                ShowMessage("没有选中任何记录");
                return;
            }
            FrmCfgReportEdit frm = new FrmCfgReportEdit();
            var ret =
            frm.ShowDialog(new EditFormArgs
            {
                SetModel = report,
                SaveHandle = (m) => BLLObj.EditReport(m as Sys_Report),
            });
            if (ret)
            {
                report.Id = ret.data;
                dgvReport.Refresh();
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {
            var report = dgvReport.GetSelectedClassData<Sys_Report>();
            if (report == null)
            {
                ShowMessage("没有选中任何记录");
                return;
            }
            if (!ShowQuestion("确实要删除选中记录吗？", "删除确认")) return;

            var ret = BLLObj.DelReport(report.Id);
            if (ret)
            {
                dgvReport.DelDataSource(report);
            }
            ShowMessage(ret.msg);
        }

        private void tsAdSearch_Click(object sender, EventArgs e)
        {
            ADSearch(SearchArg, RefreshReportData, dgvReport);
        }

        private void tsTxtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RefreshReportData();
            }
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            SearchArg.ClearADList();
            RefreshReportData();
        }

        private void tsbtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsBtnRefeshArg_Click(object sender, EventArgs e)
        {
            RefreshArgData();
        }

        private void tsBtnAddArg_Click(object sender, EventArgs e)
        {
            var report = dgvReport.GetSelectedClassData<Sys_Report>();
            if (report == null)
            {
                ShowMessage("没有选中报表记录");
                return;
            }
            var newModel = new Sys_ReportArg();
            newModel.MainId = report.Id;
            newModel.FormName = report.FormName;
            newModel.OrderNo = dgvReportArg.RowCount + 1;
            FrmCfgReportArgEdit frm = new FrmCfgReportArgEdit();
            var ret =
            frm.ShowDialog(new EditFormArgs
            {
                SetModel = newModel,
                SaveHandle = m => BLLObj.Add(m as Sys_ReportArg)
            });
            if (ret)
            {
                newModel.Id = ret.data;
                dgvReportArg.AddDataSource(newModel);
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnAddArgCopy_Click(object sender, EventArgs e)
        {
            var selModel = dgvReportArg.GetSelectedClassData<Sys_ReportArg>();
            if (selModel == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            selModel.OrderNo++;
            var newModel = new Sys_ReportArg();
            FrmCfgReportArgEdit frm = new FrmCfgReportArgEdit();
            var ret =
            frm.ShowDialog(new EditFormArgs
            {
                SetModel = selModel,
                SaveModel = newModel,
                SaveHandle = m => BLLObj.Add(m as Sys_ReportArg),
                ClearId = true
            });
            if (ret)
            {
                newModel.Id = ret.data;
                dgvReportArg.AddDataSource(newModel);
                ShowMessage(ret.msg);
            }
            selModel.OrderNo--;
        }

        private void tsBtnEditArg_Click(object sender, EventArgs e)
        {
            var selModel = dgvReportArg.GetSelectedClassData<Sys_ReportArg>();
            if (selModel == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            FrmCfgReportArgEdit frm = new FrmCfgReportArgEdit();
            var ret =
            frm.ShowDialog(new EditFormArgs
            {
                SetModel = selModel,
                //SaveHandle = BLLObj.Update,
                SaveHandle = m => BLLObj.Update(m as Sys_ReportArg),
                IsEditShow = true
            });
            if (ret)
            {
                dgvReportArg.Refresh();
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnDelArg_Click(object sender, EventArgs e)
        {
            var selModel = dgvReportArg.GetSelectedClassData<Sys_ReportArg>();
            if (selModel == null)
            {
                ShowMessage("没有选中记录");
                return;
            }

            if (!ShowQuestion("确实要删除选中记录吗？", "删除确认")) return;
            var ret = BLLObj.Del(selModel);
            if (ret)
            {
                dgvReportArg.DelDataSource(selModel);
            }
            ShowMessage(ret.msg);
        }

        private void dgvReport_SelectionChangedAndCellClick(object sender, DataGridViewCellEventArgs e)
        {
            RefreshArgData();
        }
    }
}